package com.lykj.llw.box.ui.activity

import android.graphics.Color
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.KeyboardUtils
import com.blankj.utilcode.util.LogUtils
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.constants.BConstants
import com.lykj.lib_base.data.bean.response.UserInfoResponse
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.utils.ToastUtils
import com.lykj.lib_base.net.http.CustomResponseBodyObserver
import com.lykj.lib_base.net.http.RetrofitManager
import com.lykj.llw.box.R
import com.lykj.llw.box.databinding.ActWelfareExchangeBinding
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody

/**
 * ================================================
 * 福利兑换页面
 * ================================================
 *
 * @author gosha
 * @date 2023/10/17-15:57
 */
class WelfareExchangeActivity : BaseActivity() {
    lateinit var binding: ActWelfareExchangeBinding

    private var mTextWatcher: TextWatcher? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActWelfareExchangeBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initUI()
        initData()
        setListener()
    }

    override fun onResume() {
        super.onResume()

        ImmersionBar.with(this)
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()
    }

    override fun initUI() {
        binding.titleBar.root.setBackgroundColor(Color.TRANSPARENT)
        binding.titleBar.tvTitle.text = "福利兑换"
        binding.titleBar.tvRightOption.text = "兑换记录"
        binding.titleBar.tvRightOption.setTextColor(ColorUtils.getColor(R.color.black_FF242424))

        if (!BConstants.isDebug) {
            binding.titleBar.tvRightOption.visibility = View.INVISIBLE
        }
    }

    //福利兑换码
    private fun welfareCodeExchange(code: String) {
        clearBodyParam()
        initBodyParam()
        bodyParams["vipCode"] = code
        RetrofitManager.instance.welfareCodeExchange(bodyParams,
            object :
                CustomResponseBodyObserver<ResponseBody>() {
                override fun onSubscribe(d: Disposable) {
                    addDisposables(this, d)
                }

                override fun specificHandle(responseBody: String) {
                    ToastUtils.showShort(this@WelfareExchangeActivity, "兑换成功")
                }

                override fun handleException(ret: String, msg: String?) {
                    msg?.let {
                        ToastUtils.showShort(this@WelfareExchangeActivity, it)
                    }
                }
            })
    }

    override fun initData() {
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }

        mTextWatcher = object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                if (s?.length ?: 0 > 0) {
                    binding.tvBtnExchange.setBackgroundResource(R.drawable.shape_half_ff664fc3)
                } else {
                    binding.tvBtnExchange.setBackgroundResource(R.drawable.shape_half_ff7d7c7a)
                }
            }
        }
        binding.etCdk.addTextChangedListener(mTextWatcher)

        binding.tvBtnExchange.clickWithTrigger {
            if (binding.etCdk.text.isNullOrBlank()) {
                ToastUtils.showShort(this@WelfareExchangeActivity, "请先输入福利兑换码")
                return@clickWithTrigger
            }
            welfareCodeExchange(binding.etCdk.text.toString())
        }

        KeyboardUtils.registerSoftInputChangedListener(this) { height ->
            if (height == 0) {//软键盘收起,移除焦点
                binding.etCdk.clearFocus()
            }
        }
    }

    override fun onDestroy() {
        binding.etCdk.removeTextChangedListener(mTextWatcher)
        removeDisposables(this)
        super.onDestroy()
    }
}